package com.koron.order.defect.controller;

import com.koron.bean.base.Response;
import com.koron.bean.system.org.OrgVo;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.order.common.bean.po.OrderReassignPo;
import com.koron.order.defect.bean.po.DefectStatus;
import com.koron.order.defect.bean.vo.DispatchVO;
import com.koron.order.defect.service.DispatchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @ClassName OrderController
 * @description
 * @Author zhouj
 * @Date 2021/9/1 11:26
 */
@Slf4j
@RequestMapping("/defect/dispatch/")
@RestController
@Api(tags = "缺陷派工信息")
public class DefectDispatchController {

    public String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    public EamUser getCurrUser() {
        return ThreadLocalContext.get();
    }

    @Autowired
    DispatchService dispatchService;

    @Autowired
    DefectController defectController;

    @GetMapping("queryDispatchInfoByOrderId")
    @ApiOperation(value = "查询单个派工信息")
    public Response<DispatchVO> queryDispatchInfoByOrderId(DispatchVO dispatchVO) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), dispatchService, "querySingle", DispatchVO.class, dispatchVO));
    }

    @PostMapping("saveDispatchInfo")
    @ApiOperation(value = "保存派工信息")
    public Response<String> saveDispatchInfo(@RequestBody DispatchVO dispatchVO) {
        //校验当前用户是否和工单创建人属于同一单位

        defectController.verityStatus(dispatchVO.getOrderId(), DefectStatus.WAIT_DISPATCH.getStatus());
        return Response.ok(ADOConnection.runTask(getCurrDs(), dispatchService, "saveDispatch", String.class, dispatchVO));
    }

    @ApiOperation(value = "根据orderId删除")
    @DeleteMapping("deleteDispatchInfo")
    public Response<Integer> deleteDispatchInfo(@NotBlank @RequestParam String orderId) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), dispatchService, "deleteByOrderId", Integer.class, orderId));
    }

    @ApiOperation(value = "转派工单")
    @PostMapping("reassign")
    public Response<OrderReassignPo> reassign(@RequestBody OrderReassignPo orderReassignPo) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), dispatchService, "reassign", OrderReassignPo.class, orderReassignPo));
    }

    //校验操作权限  同组织才能操作
    Boolean verityOperation(String deptId) {
        if (StringUtils.isEmpty(deptId)) {
            return true;
        }

        List<OrgVo> orgVoList = getCurrUser().getOrgVoList();
        if (CollectionUtils.isEmpty(orgVoList)) {
            return false;
        }
        for (OrgVo orgVo : orgVoList) {
            if (orgVo.getId().equals(deptId)) {
                return true;
            }
        }
        return false;

    }

}
